home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / lib / rcscripts / net.modules.d / helpers.d / functions < prev    next >
Encoding:
Text File  |  2006-04-25  |  2.2 KB  |  117 lines

  1. # Copyright (c) 2004-2005 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # $Header$
  4.  
  5. # Contributed by Roy Marples (uberlord@gentoo.org)
  6.  
  7. # int netmask2cidr(char *netmask)
  8. #
  9. # Returns the CIDR of a given netmask
  10. netmask2cidr() {
  11.     local binary="" i bin
  12.  
  13.     for i in ${1//./ }; do
  14.         bin=""
  15.         while [[ ${i} != 0 ]]; do
  16.             bin=$[${i}%2]${bin}
  17.             (( i=i>>1 ))
  18.         done
  19.         binary="${binary}${bin}"
  20.     done
  21.     binary=${binary%%0*}
  22.     echo ${#binary}
  23. }
  24.  
  25. # char* netmask2cidr(int cidr)
  26. #
  27. # Returns the netmask of a given CIDR
  28. cidr2netmask() {
  29.     local cidr=${1} netmask="" done=0 i sum=0 cur=128
  30.     local octets frac
  31.  
  32.     (( octets=cidr/8 ))
  33.     (( frac=cidr%8 ))
  34.     while [[ octets -gt 0 ]]; do
  35.         netmask="${netmask}.255"
  36.         (( octets-- ))
  37.         (( done++ ))
  38.     done
  39.  
  40.     if [[ ${done} -lt 4 ]]; then
  41.         for (( i=0; i<${frac}; i++ )); do
  42.             (( sum+=cur ))
  43.             (( cur/=2 ))
  44.         done
  45.         netmask="${netmask}.${sum}"
  46.         (( done++ ))
  47.         
  48.         while [[ ${done} -lt 4 ]]; do
  49.             netmask="${netmask}.0"
  50.             (( done++ ))
  51.         done
  52.     fi
  53.  
  54.     echo ${netmask:1}
  55. }
  56.  
  57. # char* interface_device(char *iface)
  58. #
  59. # Gets the base device of the interface
  60. # Can handle eth0:1 and eth0.1
  61. # Which returns eth0 in this case
  62. interface_device() {
  63.     local dev=${1%%.*}
  64.     [[ ${dev} == ${1} ]] && dev=${1%%:*}
  65.     echo ${dev}
  66. }
  67.  
  68. # char* interface_type(char* iface)
  69. #
  70. # Returns the base type of the interface
  71. # eth, ippp, etc
  72. interface_type() {
  73.     echo ${1%%[0-9]*}
  74. }
  75.  
  76. # char* interface_variable(char *iface)
  77. #
  78. # Returns a bash variable name based on the interface
  79. interface_variable() {
  80.     LC_ALL=C echo ${1//[![:word:]]/_}
  81. }
  82.  
  83. # bool clean_pidfile(char *file)
  84. #
  85. # Removes the given pidfile if the process is not running
  86. # Returns 1 if the process is still running otherwise 0
  87. clean_pidfile() {
  88.     local pidfile=${1}
  89.  
  90.     [[ ! -f ${pidfile} ]] && return 0
  91.     local pid=$( cat ${pidfile} )
  92.  
  93.     if [[ -n ${pid} ]]; then
  94.         local cmd=${pidfile##*/}
  95.         cmd=${cmd%%-*}
  96.         ps -p ${pid} 2>/dev/null | grep -q ${cmd} && return 1
  97.     fi
  98.  
  99.     rm -f ${pidfile}
  100.     return 0
  101. }
  102.  
  103. # bool process_finished(int pid, char* cmd)
  104. #
  105. # We wait for 10 seconds until the command ${cmd}
  106. # stops running on the process ${pid}
  107. process_finished() {
  108.     local i pid=${1} cmd=${2} secs=${3:-9}
  109.  
  110.     for (( i=0; i<secs; i++ )); do
  111.         ps -p ${pid} | grep -q ${cmd} || return 0
  112.         sleep 1
  113.     done
  114.  
  115.     return 1
  116. }
  117.